/** @format */

// [
//   ({
//     progress: _.gt(10)
//   },
//   {
//     createTime: _.lt(5766666)
//   })
// ]
const a = {
  _id: 'fff'
}

const db = () => () => {}

function formatDBQuery(originQuery) {
  const _ = () => () => {}

  originQuery = Object.assign({}, originQuery)
  originQuery.$where && delete originQuery.$where
  const map = {
    $gt: _.gt,
    $gte: _.gte,
    $lt: _.lt,
    $lte: _.lte,
    $in: _.in,
    $nin: _.nin,
    $eq: _.eq,
    $neq: _.neq,
    $exists: _.exists,
    $or: _.or,
    $and: _.and
  }
  const query = Object.keys(originQuery).map(queryKey => {
    return typeof originQuery[queryKey] === 'object'
      ? Object.keys(originQuery[queryKey]).map(operator => {
          return {
            [queryKey]:
              operator === '$regex'
                ? db.RegExp({
                    regexp: originQuery?.[queryKey]?.[operator],
                    options: 'i'
                  })
                : map?.[operator]?.(originQuery?.[queryKey]?.[operator])
          }
        })
      : [{ [queryKey]: originQuery[queryKey] }]
  })

  return [].concat(...query)
  // return Object.keys(originQuery).map(queryKey => {
  //   return typeof originQuery[queryKey] === 'object'
  //     ? Object.keys(originQuery[queryKey]).map(operator => {
  //         return { queryKey: map?.[operator]?.(originQuery?.[queryKey]?.[operator]) }
  //       })
  //     : [{ originQuery: originQuery[queryKey] }]
  // })
}

console.log('formatDBQuery() :>> ', formatDBQuery(a))
